package com.wsz.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wsz.pojo.FriendId;
import com.wsz.pojo.R;
import com.wsz.pojo.UserInfo;
import com.wsz.user.service.FriendIdService;
import com.wsz.user.service.UserInfoService;
import com.wsz.utils.SecurityContextUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@AllArgsConstructor
@RestController
public class UserInfoController {

    private final FriendIdService friendIdService;
    private final UserInfoService userInfoService;

    @GetMapping("/ranking/list/{page}/{limit}")
    public R RankingList(@PathVariable Long page,
                         @PathVariable Long limit){
        return userInfoService.getRanks(page,limit);
    }

    @GetMapping("/follower")
    public R follower(@RequestParam("fId") String fId){
        String mId = SecurityContextUtils.getUserId();
        FriendId flag = friendIdService.getOne(new QueryWrapper<FriendId>().eq("u_id", mId).eq("f_id", fId));
        if(flag == null) return R.ok().data("flag",true);
        return R.ok().data("flag",false);
    }

    @PostMapping("/follower")
    public R fowllower(@RequestParam("fId") String fId){
        String mId = SecurityContextUtils.getUserId();

        if(fId == null){
            return R.error().message("数据不合法");
        }
        if(mId.equals(fId)){
            return R.error().message("不能关注自己哦！");
        }else{
            FriendId flag = friendIdService.getOne(new QueryWrapper<FriendId>().eq("u_id", mId).eq("f_id", fId));

            if(flag != null){
                return R.error().message("已经关注过了！");
            }

            UserInfo bf = userInfoService.getById(fId);

            if(bf == null) return R.error().message("关注用户不存在");


            UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",fId);
            updateWrapper.setSql("follower_count = follower_count + 1");


            UpdateWrapper<UserInfo> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.eq("id",mId);
            updateWrapper1.setSql("concern_count = concern_count + 1");

            userInfoService.update(updateWrapper);
            userInfoService.update(updateWrapper1);

            FriendId friendId = new FriendId();
            friendId.setFId(fId);
            friendId.setUId(mId);
            friendIdService.save(friendId);
            return R.ok();
        }
    }

    @PutMapping("/follower")
    public R notFollower(@RequestParam("fId") String fId){
            String mId = SecurityContextUtils.getUserId();

            if(fId == null || mId.equals(fId)){
                return R.error().message("数据不合法");
            }

            QueryWrapper<FriendId> eq = new QueryWrapper<>();
            eq.eq("u_id", mId).eq("f_id", fId);

            FriendId flag = friendIdService.getOne(eq);

            if(flag == null){
                return R.error().message("没有关注过了！");
            }

            UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",fId);
            updateWrapper.setSql("follower_count = follower_count - 1");


            UpdateWrapper<UserInfo> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.eq("id",mId);
            updateWrapper1.setSql("concern_count = concern_count - 1");

            userInfoService.update(updateWrapper);
            userInfoService.update(updateWrapper1);

            friendIdService.remove(eq);
            return R.ok();
    }
}
